#
#Extracts contacts from processed CSV files as vCard(vcf) which can then be exported into any phone
#Version 0.1 Anand 20101111
use strict;


my $contactFile = "conout.csv";
my $numberFile = "numout.csv";

open(CONTACTFILE, $contactFile);
open(NUMBERFILE, $numberFile);



while(<CONTACTFILE>)
{
  my @fields = split ',';
  my $name =$fields[1];
  
  open(OUTPUT,">$name.vcf");
  
  print OUTPUT "BEGIN:VCARD\nVERSION:2.1\n";
  print OUTPUT "N:;$name;;;\n";
  for(my $i = 0; $i< $fields[2];$i++)
  {
     my $numberLine = <NUMBERFILE>;
	 my @numberFields = split ',', $numberLine;
     if($numberFields[0] != $fields[0])
     {
	    die "NON Matching Ids Found, Data not sorted properly !!!!";
     }
     
	 my $number = $numberFields[1];
	 chomp $number;
     print OUTPUT "TEL;CELL:$number\n";   	 
  }
  print OUTPUT "X-CLASS:private\nEND:VCARD\n";
  
  close OUTPUT;
}

close CONTACTFILE;
close NUMBERFILE;
